package apptoko.com;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Toast;
import apptoko.com.adapter.ErrorHandler;
import apptoko.com.background.BackgroundTasks;
import apptoko.com.event.Event.BuyFailEvent;
import apptoko.com.event.Event.BuySuccessEvent;
import apptoko.com.event.Event.UserInfoFailEvent;
import apptoko.com.event.Event.UserInfoSuccessEvent;
import apptoko.com.network.ProgressDialogManager;
import apptoko.com.util.Constants;
import apptoko.com.util.Utils;

import com.androidquery.AQuery;
import apptoko.com.R;
import com.google.analytics.tracking.android.EasyTracker;

import de.greenrobot.event.EventBus;

public class ConfirmBuyActivity extends Activity implements OnClickListener {

	private String accessToken;
	private long versionId;
	private String slug;
	private Bundle bundle;
	private String userName = "";
	private BackgroundTasks bgTask;
	private AQuery aq;
	private ProgressDialog pDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_confirm_buy);
		aq = new AQuery(this);
		bgTask = new BackgroundTasks(this);
		bundle = getIntent().getExtras();
		aq.id(R.id.btn_ok).clicked(this);
		aq.id(R.id.btn_cancel).clicked(this);
		EasyTracker.getInstance().setContext(this);
		getTym();
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		EventBus.getDefault().unregister(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		EventBus.getDefault().register(this);
	}

	private void getTym() {
		pDialog = ProgressDialogManager.showProgressDialog(this, R.string.loading);
		String accessToken = Utils.getStaticAccessToken(this);
		if(accessToken!="3de71f9e84a325bbfb86466c15b40b1e"){
			bgTask.getUserInfo(accessToken);
		}
	}
	
	public void onEventMainThread(UserInfoSuccessEvent event){
		if(pDialog != null){
			pDialog.dismiss();
		}
		userName = event.getUser().getUserName();
		aq.id(R.id.green_tym).text(String.valueOf(event.getUser().getGreenTym()));
		aq.id(R.id.purple_tym).text(String.valueOf(event.getUser().getPurpleTym()));
		aq.id(R.id.yellow_tym).text(String.valueOf(event.getUser().getYellowTym()));
		aq.id(R.id.image).image(bundle.getString(Constants.KEY_IMAGE), false, true);
		aq.id(R.id.content_name).text(bundle.getString(Constants.KEY_NAME));
		aq.id(R.id.content_author).text(bundle.getString(Constants.KEY_AUTHOR));
		aq.id(R.id.content_version).text(bundle.getString(Constants.KEY_EP));
		int price1 = bundle.getInt(Constants.KEY_PRICE_1);
		int price2 = bundle.getInt(Constants.KEY_PRICE_2);
		if(price1 != 0 && price2 == 0){
			aq.id(R.id.price).text(String.format(getResources().getString(R.string.with_price), price1));
			aq.id(R.id.tym).image(R.drawable.tym_green);
		} else if(price1 == 0 && price2 != 0){
			aq.id(R.id.price).text(String.format(getResources().getString(R.string.with_price), price2));
			aq.id(R.id.tym).image(R.drawable.tym_purple);
		}
		versionId = bundle.getLong(Constants.KEY_VERSION);
		slug = bundle.getString(Constants.KEY_SLUG);
	}
	
	public void onEventMainThread(UserInfoFailEvent event){
		if(pDialog != null){
			pDialog.dismiss();
		}
		ErrorHandler.getInstance().setContext(this).handleError(event.getErrorCode());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_ok:
			EasyTracker.getInstance().setContext(ConfirmBuyActivity.this);
			EasyTracker.getTracker().sendEvent(Constants.EVENT, Constants.BUY_EVENT_ACTION, userName + " buy " + bundle.getString(Constants.KEY_NAME), Long.valueOf(10));
			pDialog = ProgressDialogManager.showProgressDialog(this, R.string.loading);
			accessToken = Utils.getStaticAccessToken(this);
			bgTask.buyContent("", accessToken, slug, (int)versionId);
			break;
		case R.id.btn_cancel:
			setResult(RESULT_CANCELED);
			finish();
			break;
		}
	}
	
	public void onEventMainThread(BuySuccessEvent event){
		if(pDialog != null){
			pDialog.dismiss();
		}
		Intent i = getIntent();
		Bundle b = new Bundle();
		b.putString(Constants.SLUG, slug);
		b.putLong(Constants.KEY_VERSION, versionId);
		i.putExtras(b);
		setResult(RESULT_OK, i);
		finish();
	}
	
	public void onEventMainThread(BuyFailEvent event){
		if(pDialog != null){
			pDialog.dismiss();
		}
		Toast.makeText(this, event.getMessage(), Toast.LENGTH_SHORT).show();
	}
//	
//	public void onEventMainThread(RequestFailEvent event){
//		if(pDialog != null){
//			pDialog.dismiss();
//		}
//		ErrorHandler.getInstance().setContext(this).handleError(event.getErrorCode());
//	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		setResult(RESULT_CANCELED);
		super.onBackPressed();
	}
}
